package com.itdct.onflow.core.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/**
 * @author DCTANT
 * @date 2021/11/9 23:56:26
 * @description
 */
public class StringUtil {
    private static List<String> specialWords = Arrays.asList("%", "_", "\\");

    public static String likeFuzzy(String param) {
        if (StringUtils.isNotBlank(param)) {
            StringBuilder stringBuilder = new StringBuilder();
            String[] split = param.split("");
            for (String str : split) {
                if (specialWords.contains(str)) {
                    stringBuilder.append("\\").append(str);
                } else {
                    stringBuilder.append(str);
                }
            }
            return "%" + stringBuilder.append("%").toString();
        }
        return null;
    }

    public static String listToInList(List list) {
        if (list == null || list.size() == 0) {
            return null;
        }
        Object obj = list.get(0);
        if (obj instanceof CharSequence) {
            String inString = "(" + String.join(",", list) + ")";
            return inString;
        } else {
            ArrayList<String> strings = new ArrayList<>();
            for (Object o : list) {
                strings.add(o.toString());
            }
            String inString = "(" + String.join(",", list) + ")";
            return inString;
        }

    }

    /**
     * 下划线转驼峰
     * user_name  ---->  userName
     * userName   --->  userName
     *
     * @param underlineStr 带有下划线的字符串
     * @return 驼峰字符串
     */
    public static String toCamelCase(String underlineStr) {
        if (underlineStr == null) {
            return null;
        }
        // 分成数组
        char[] charArray = underlineStr.toCharArray();
        // 判断上次循环的字符是否是"_"
        boolean underlineBefore = false;
        StringBuffer buffer = new StringBuffer();
        for (int i = 0, l = charArray.length; i < l; i++) {
            // 判断当前字符是否是"_",如果跳出本次循环
            if (charArray[i] == 95) {
                underlineBefore = true;
            } else if (underlineBefore) {
                // 如果为true，代表上次的字符是"_",当前字符需要转成大写
                buffer.append(charArray[i] -= 32);
                underlineBefore = false;
            } else {
                // 不是"_"后的字符就直接追加
                buffer.append(charArray[i]);
            }
        }
        return buffer.toString();
    }

    public static String toBigCamelCase(String underlineStr) {
        String camelCase = toCamelCase(underlineStr);
        if (StringUtils.isNotBlank(camelCase)) {
            String first = camelCase.substring(0, 1);
            String tail = camelCase.substring(1);
            first = first.toUpperCase(Locale.ROOT);
            return first + tail;
        }
        return null;
    }

    public static String toSmallCamelCase(String underlineStr) {
        String camelCase = toCamelCase(underlineStr);
        if (StringUtils.isNotBlank(camelCase)) {
            String first = camelCase.substring(0, 1);
            String tail = camelCase.substring(1);
            first = first.toLowerCase(Locale.ROOT);
            return first + tail;
        }
        return null;
    }

    /**
     * 驼峰转 下划线
     * userName  ---->  user_name
     * user_name  ---->  user_name
     *
     * @param camelCaseStr 驼峰字符串
     * @return 带下滑线的String
     */
    public static String toUnderlineCase(String camelCaseStr) {
        if (camelCaseStr == null) {
            return null;
        }
        // 将驼峰字符串转换成数组
        char[] charArray = camelCaseStr.toCharArray();
        StringBuffer buffer = new StringBuffer();
        //处理字符串
        for (int i = 0, l = charArray.length; i < l; i++) {
            if (charArray[i] >= 65 && charArray[i] <= 90) {
                buffer.append("_").append(charArray[i] += 32);
            } else {
                buffer.append(charArray[i]);
            }
        }
        return buffer.toString();
    }

}
